<template>
  <div class="page-search">
    <cc-form class="cc-form" v-bind="searchFormConfig" v-model="formData">
      <template #header>
        <h1>高级检索</h1>
      </template>
      <template #footer>
        <div class="handle-btns">
          <el-button size="mini" type="default" icon="el-icon-refresh-right" @click="handleResetClick">重置</el-button>
          <el-button size="mini" type="primary" icon="el-icon-search" @click="handleQueryClick">搜索</el-button>
        </div>
      </template>
    </cc-form>
  </div>
</template>

<script lang="ts">
import { defineComponent, ref } from 'vue'
import { ElButton } from 'element-plus'
import { CcForm } from '@/base-ui/form'

export default defineComponent({
  props: {
    searchFormConfig: {
      type: Object,
      required: true
    }
  },
  components: { CcForm, ElButton },
  emits: ['resetBtnClick', 'queryBtnClick'],
  setup(props, { emit }) {
    const formItems = props.searchFormConfig.formItems ?? []
    const formOriginData: any = {}
    for (const item of formItems) {
      formOriginData[item.field] = ''
    }
    const formData = ref(formOriginData)

    const handleResetClick = () => {
      // 弃用二： 匹配 @/base-ui/form 中的弃用二
      // for (const key in formOriginData) {
      //   formData.value[`${key}`] = formOriginData[key]
      // }

      formData.value = formOriginData
      emit('resetBtnClick')
    }

    // 搜索
    const handleQueryClick = () => {
      emit('queryBtnClick', formData.value)
    }

    return { formData, handleResetClick, handleQueryClick }
  }
})
</script>
<style scoped lang="less">
.handle-btns {
  text-align: right;
  padding: 0 50px 20px 0;
}
</style>
